--
-- NewDailyTaskSubRender
-- Created by IntelliJ IDEA.
-- User: yao.chen
-- Date: 2019/1/3
--

local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end

function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init()
    self._refreshBtn = self:findChild("btn_refresh")
    self._refreshBtn:addClickEventListener(function()
        self._refreshBtn:setVisible(false) -- 防止连续点击
        executeFunc(self._onClick, "refreshDailyTask", self._data)
    end)

    self:refresh()
end

function ClassRef:setDropItemNode(root, dropItem)
    if dropItem then
        root:setVisible(true)
        local uiItem = IconRender.create(root)
        uiItem:setData(dropItem)
        uiItem:setTipEnable(true)
    else
        root:setVisible(false)
    end
end

function ClassRef:refresh()
    if not self._data then return end
    local task = self._data
    local taskId = task:getId()

    local panelBG = self:getChildByName("Panel_bg")
    local btnItem = panelBG:getChildByName("bg01")
    btnItem:setZoomScale(0)

    local rewardNode = self:getChildByName("icon_reward")
    local dropLists = task:getRewards()
    self:setDropItemNode(rewardNode, dropLists[1])

    local iconOverNode = self:getChildByName("icon_over")
    local btnGo = self:getChildByName("Button_go")
    local btnGet = self:getChildByName("Button_lingqu")
    local nameNode = self:getChildByName("name")
    local desNode = self:getChildByName("Text")
    local numNode = self:getChildByName("num")
    numNode:setVisible(false)
    btnGo:setVisible(false)
    btnGet:setVisible(false)
    iconOverNode:setVisible(false)

    local btnName = btnGo:getChildByName("name")
    btnName:setString(L("task_daily_desc1"))
    local taskConfig = task:getConfig()
    local qualityColor = Enums.QualityColor[tonumber(taskConfig.quality) or 1]
    nameNode:setColor(qualityColor)
    nameNode:setString(L(taskConfig.name))
    desNode:setString(L(taskConfig.target))

    if task:isTaken() then
        btnGo:setVisible(true)
        -- 设置进度
        local doneProgress = task:getDefaultDone()
        local totalProgress = task:getDefaultTotal()
        numNode:setVisible(true)
        numNode:setString(doneProgress .. "/" .. totalProgress)
    elseif task:isFinished() then
        btnGet:setVisible(true)
    elseif task:isRewarded() then
        iconOverNode:setVisible(true)
    end

    btnGo:addClickEventListener(function(sender)
        if tonumber(taskConfig.scheduleString) == 5 then -- 界面追踪
            local resource = GD:getSpecialResource(taskConfig.autoRun)
            if resource then
                if resource[1] and resource[1] ~= "nil" then
                    display.resourceGotoFeature({feature = resource[1], param = resource[3]})
                else
                    local  funcId = tonumber(taskConfig.autoRun)
                    if funcId then
                        display.resourceGotoFeature({feature = funcId})
                    end
                end
            end
        else
            self._onClick("TASK_AUTO_WAY", {findNpcId = taskConfig.autoRun, trackTaskId = taskId})
        end
    end)

    btnGet:addClickEventListener(function(sender)
        btnGet:setVisible(false) -- 防止连续点击
        self._onClick("GET_TASK_REWARD", {taskId = taskId, taskType = "daily"})
    end)

    -- 每日任务 并且是taken状态，显示刷新按钮
    self._refreshBtn:setVisible(false)
    if task:isTaken() then
        local pos = task:getPos()
        local refreshNum = TaskManager:getDailyTaskRefreshNum(pos)
        if refreshNum > 0 then
            self._refreshBtn:setVisible(true)
        end
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef